package com.commonlib.downloadmgr.util;

import java.io.File;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;
import android.util.Log;

public class Utility {
	private static final String TAG = Utility.class.getSimpleName();
	
	/**
	 * 
	 * @param context
	 * @param packagename
	 * @param verCode
	 * @return
	 */
	public static boolean isPackageAvailable(Context context,String packagename,String verCode){
		boolean isAvailabel = false;  
		PackageInfo packageInfo = null;
	      try {
	          packageInfo = context.getPackageManager().getPackageInfo(
	                  packagename, 0);
	      } catch (NameNotFoundException e) {
	          packageInfo = null;
	          Log.w(TAG, "not find PackageInfo");
	      }
	      if(packageInfo != null && verCode != null){
	    	  isAvailabel = verCode.equals(packageInfo.versionCode) ? true : false;
	      }
	      return isAvailabel;
	}
	
	public static void installApk(Context context, File file){
		Intent intent = new Intent(Intent.ACTION_VIEW);
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		Uri puri = Uri.fromFile(file);
		intent.setDataAndType(puri,
				"application/vnd.android.package-archive");
		context.startActivity(intent);
	}
	
	public static void openApp(Context context, String pkgName){
		Intent intent = context.getPackageManager().getLaunchIntentForPackage(pkgName);
		context.startActivity(intent);
	}
}
